#!/bin/env python
#coding:utf-8
import sys
reload(sys)
sys.setdefaultencoding('utf8')

from email.utils import formatdate
import hmac,hashlib,base64
import requests, json, time, datetime

class Auth(object):
    def __init__(self, accessKey, secretKey):
        self.accessKey = accessKey
        self.secretKey = secretKey.encode('UTF-8')

    def sign_request(self, method, resource):
        string_to_sign = '{0}\n{1}\n{2}\n{3}\n{4}{5}'.format(
            method,
            '',
            'application/json',
            formatdate(None, usegmt=True),
            '',
            resource,
        )

        string_to_sign = string_to_sign.encode('UTF-8')

        h = hmac.new(self.secretKey, string_to_sign, hashlib.sha1)
        signature = base64.urlsafe_b64encode(h.digest()).strip()
        signature = signature.decode('UTF-8')
        # authorization = 'Pandora {0}:{1}'.format(accessKey, signature)
        authorization = self.accessKey + ':' + signature
        return authorization